<div class="panel-collapse collapse in" id="collapse-payment-address" aria-expanded="true" style="">
  <div class="panel-body">
    <form class="form-horizontal">
      <div class="radio">
        <label>
        <input type="radio" name="payment_address" value="existing" checked="checked">
          使用現存地址
        </label>
      </div>
      <div id="payment-existing">
        <select name="address_id" class="form-control">
        <option value="3" selected="selected">erwer, Hong Kong, Sha Tin New Territories, dsfds, , ertretre, 3213213</option>
          <option value="4">aaaa, Hong Kong, Sha Tin New Territories, jiulong, , 大连甘井子区, 123232132</option>
        </select>
      </div>
      <div class="radio">
        <label>
          <input type="radio" name="payment_address" value="new">
          添加壹個新地址</label>
      </div>
    <br>
    <div id="payment-new" style="display: none;">
      <div class="form-group required">
        <label class="col-sm-2 control-label" for="input-payment-fullname">姓名</label>
        <div class="col-sm-10">
          <input type="text" name="fullname" value="" placeholder="姓名" id="input-payment-fullname" class="form-control">
        </div>
      </div>
      <div class="form-group required">
        <label class="col-sm-2 control-label" for="input-payment-shipping-telephone">收件人電話</label>
        <div class="col-sm-10">
          <input type="text" name="shipping_telephone" value="" placeholder="收件人電話" id="input-payment-shipping-telephone" class="form-control">
        </div>
      </div>

      <div class="form-group">
        <label class="col-sm-2 control-label" for="input-payment-company">公司</label>
        <div class="col-sm-10">
          <input type="text" name="company" value="" placeholder="公司" id="input-payment-company" class="form-control">
        </div>
      </div>

      <div class="form-group required">
        <label class="col-sm-2 control-label" for="input-payment-country">國家</label>
        <div class="col-sm-10">
          <select name="country_id" id="input-payment-country" class="form-control">
            <option value="">--- 請選擇 --- </option>
            <option value="244">Aaland Islands</option>
                  <option value="1">Afghanistan</option>
                  <option value="44">中国</option>
          </select> 
        </div>
      </div>

      <div class="form-group required">
        <label class="col-sm-2 control-label" for="input-payment-zone">州/省/地區</label>
        <div class="col-sm-10">
          <select name="zone_id" id="input-payment-zone" class="form-control"><option value="">--- 請選擇 --- </option>
          </select> 
        </div>
      </div>
      <div class="form-group required" id="payment-china-city" style="display: none;">
        <label class="col-sm-2 control-label" for="input-payment-city">所在城市</label>
        <div class="col-sm-10">
          <select name="city_id" id="input-payment-city" class="form-control"><option value="">--- 請選擇 --- </option><option value="0" selected="selected">--- 無 --- </option></select> 
        </div>
      </div>

      <div class="form-group required" id="payment-china-district" style="display: none;">
        <label class="col-sm-2 control-label" for="input-payment-district">區縣</label>
        <div class="col-sm-10">
          <select name="district_id" id="input-payment-district" class="form-control"><option value="">--- 請選擇 --- </option><option value="0" selected="selected">--- 無 --- </option></select>
        </div>
      </div>
      <div class="form-group required" id="payment-world-city">
        <label class="col-sm-2 control-label" for="input-payment-city">所在城市</label>
        <div class="col-sm-10">
          <input type="text" name="city" value="" placeholder="所在城市" id="input-payment-city" class="form-control">
        </div>
      </div>
      <div class="form-group required">
        <label class="col-sm-2 control-label" for="input-payment-address">地址</label>
        <div class="col-sm-10">
          <input type="text" name="address" value="" placeholder="地址" id="input-payment-address" class="form-control">
        </div>
      </div>

      <div class="form-group">
        <label class="col-sm-2 control-label" for="input-payment-postcode">郵政編碼</label>
        <div class="col-sm-10">
          <input type="text" name="postcode" value="" placeholder="郵政編碼" id="input-payment-postcode" class="form-control">
        </div>
      </div>
    </div>

    <div class="buttons clearfix">
      <div class="pull-right">
        <input type="button" value="繼續" id="button-payment-address" data-loading-text="加載中......" class="btn btn-primary">
      </div>
    </div>

</form>


<script type="text/javascript">
  $('input[name=\'payment_address\']').on('change', function() {
    if (this.value == 'new') {
      $('#payment-existing').hide();
      $('#payment-new').show();
    } else {
      $('#payment-existing').show();
      $('#payment-new').hide();
    }
  });
</script>

<script type="text/javascript">
  $('#collapse-payment-address .form-group[data-sort]').detach().each(function() {
    if ($(this).attr('data-sort') >= 0 && $(this).attr('data-sort') <= $('#collapse-payment-address .form-group').length-2) {
      $('#collapse-payment-address .form-group').eq(parseInt($(this).attr('data-sort'))+2).before(this);
    }

    if ($(this).attr('data-sort') > $('#collapse-payment-address .form-group').length-2) {
      $('#collapse-payment-address .form-group:last').after(this);
    }

    if ($(this).attr('data-sort') == $('#collapse-payment-address .form-group').length-2) {
      $('#collapse-payment-address .form-group:last').after(this);
    }

    if ($(this).attr('data-sort') < -$('#collapse-payment-address .form-group').length-2) {
      $('#collapse-payment-address .form-group:first').before(this);
    }
  });
</script>

<script type="text/javascript">
  $('#collapse-payment-address button[id^=\'button-payment-custom-field\']').on('click', function() {
    var node = this;
    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');

    if (typeof timer != 'undefined') {
      clearInterval(timer);
    }

    timer = setInterval(function() {
      if ($('#form-upload input[name=\'file\']').val() != '') {
        clearInterval(timer);

        $.ajax({
          url: 'index.php?route=tool/upload',
          type: 'post',
          dataType: 'json',
          data: new FormData($('#form-upload')[0]),
          cache: false,
          contentType: false,
          processData: false,
          beforeSend: function() {
            $(node).button('loading');
          },
          complete: function() {
            $(node).button('reset');
          },
          success: function(json) {
            $(node).parent().find('.text-danger').remove();

            if (json['error']) {
              $(node).parent().find('input[name^=\'custom_field\']').after('<div class="text-danger">' + json['error'] + '</div>');
            }

            if (json['success']) {
              alert(json['success']);

              $(node).parent().find('input[name^=\'custom_field\']').val(json['code']);
            }
          },
          error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
        });
      }
    }, 500);
  });
</script>

<script type="text/javascript">
// $('.date').datetimepicker({
//  pickTime: false
// });

// $('.time').datetimepicker({
//  pickDate: false
// });

// $('.datetime').datetimepicker({
//  pickDate: true,
//  pickTime: true
// });
</script>
<script type="text/javascript">

$('#collapse-payment-address select[name=\'country_id\']').on('change', function() {
  if (this.value == 44) {
    $('#payment-world-city').hide();
    $('#payment-china-city').show();
    $('#payment-china-district').show();
  } else {
    $('#payment-world-city').show();
    $('#payment-china-city').hide();
    $('#payment-china-district').hide();
  }
  $.ajax({
    url: 'index.php?route=checkout/checkout/country&country_id=' + this.value,
    dataType: 'json',
    beforeSend: function() {
      $('#collapse-payment-address select[name=\'country_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>');
    },
    complete: function() {
      $('.fa-spin').remove();
    },
    success: function(json) {
      if (json['postcode_required'] == '1') {
        $('#collapse-payment-address input[name=\'postcode\']').parent().parent().addClass('required');
      } else {
        $('#collapse-payment-address input[name=\'postcode\']').parent().parent().removeClass('required');
      }

      html = '<option value="">--- 請選擇 --- </option>';

      if (json['zone'] && json['zone'] != '') {
        for (i = 0; i < json['zone'].length; i++) {
          html += '<option value="' + json['zone'][i]['zone_id'] + '"';

          if (json['zone'][i]['zone_id'] == '1423') {
            html += ' selected="selected"';
          }

          html += '>' + json['zone'][i]['name'] + '</option>';
        }
      } else {
        html += '<option value="0" selected="selected">--- 無 --- </option>';
      }

      $('#collapse-payment-address select[name=\'zone_id\']').html(html);
      
      $('#collapse-payment-address select[name=\'zone_id\']').trigger('change');
    },
    error: function(xhr, ajaxOptions, thrownError) {
      alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
  });
});

$('#collapse-payment-address select[name=\'zone_id\']').on('change', function() {
  $.ajax({
    url: 'index.php?route=checkout/checkout/zone&zone_id=' + this.value,
    dataType: 'json',
    beforeSend: function() {
      $('#collapse-payment-address select[name=\'zone_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>');
    },
    complete: function() {
      $('.fa-spin').remove();
    },
    success: function(json) {

      html = '<option value="">--- 請選擇 --- </option>';

      if (json['city'] && json['city'] != '') {
        for (i = 0; i < json['city'].length; i++) {
          html += '<option value="' + json['city'][i]['city_id'] + '"';

          if (json['city'][i]['city_id'] == '0') {
            html += ' selected="selected"';
          }

          html += '>' + json['city'][i]['name'] + '</option>';
        }
      } else {
        html += '<option value="0" selected="selected">--- 無 --- </option>';
      }

      $('#collapse-payment-address select[name=\'city_id\']').html(html);
      
      $('#collapse-payment-address select[name=\'city_id\']').trigger('change');
    },
    error: function(xhr, ajaxOptions, thrownError) {
      alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
  });
});

$('#collapse-payment-address select[name=\'city_id\']').on('change', function() {
  $.ajax({
    url: 'index.php?route=checkout/checkout/city&city_id=' + this.value,
    dataType: 'json',
    beforeSend: function() {
      $('#collapse-payment-address select[name=\'city_id\']').after(' <i class="fa fa-circle-o-notch fa-spin"></i>');
    },
    complete: function() {
      $('.fa-spin').remove();
    },
    success: function(json) {

      html = '<option value="">--- 請選擇 --- </option>';

      if (json['district'] && json['district'] != '') {
        for (i = 0; i < json['district'].length; i++) {
          html += '<option value="' + json['district'][i]['district_id'] + '"';

          if (json['district'][i]['district_id'] == '0') {
            html += ' selected="selected"';
          }

          html += '>' + json['district'][i]['name'] + '</option>';
        }
      } else {
        html += '<option value="0" selected="selected">--- 無 --- </option>';
      }

      $('#collapse-payment-address select[name=\'district_id\']').html(html);
    },
    error: function(xhr, ajaxOptions, thrownError) {
      alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
    }
  });
});

//$('#collapse-payment-address select[name=\'country_id\']').trigger('change');
</script>
</div>
</div>



















<script><!--
$(document).on('change', 'input[name=\'account\']', function() {
  if ($('#collapse-payment-address').parent().find('.panel-heading .panel-title > *').is('a')) {
    if (this.value == 'register') {
      $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 2 步： 賬戶 &amp; 賬單詳情 <i class="fa fa-caret-down"></i></a>');
    } else {
      $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 2 步： 賬單地址 <i class="fa fa-caret-down"></i></a>');
    }
  } else {
    if (this.value == 'register') {
      $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('第 2 步： 賬戶 &amp; 賬單詳情');
    } else {
      $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('第 2 步： 賬單地址');
    }
  }
});

$(document).ready(function() {
    $.ajax({
        url: 'index.php?route=checkout/payment_address',
        dataType: 'html',
        success: function(html) {
            $('#collapse-payment-address .panel-body').html(html);

      $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 2 步： 賬單地址 <i class="fa fa-caret-down"></i></a>');

      $('a[href=\'#collapse-payment-address\']').trigger('click');
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Checkout
$(document).delegate('#button-account', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/' + $('input[name=\'account\']:checked').val(),
        dataType: 'html',
        beforeSend: function() {
          $('#button-account').button('loading');
    },
        complete: function() {
      $('#button-account').button('reset');
        },
        success: function(html) {
            $('.alert, .text-danger').remove();

            $('#collapse-payment-address .panel-body').html(html);

      if ($('input[name=\'account\']:checked').val() == 'register') {
        $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 2 步： 賬戶 &amp; 賬單詳情 <i class="fa fa-caret-down"></i></a>');
      } else {
        $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 2 步： 賬單地址 <i class="fa fa-caret-down"></i></a>');
      }

      $('a[href=\'#collapse-payment-address\']').trigger('click');
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Login
$(document).delegate('#button-login', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/login/save',
        type: 'post',
        data: $('#collapse-checkout-option :input'),
        dataType: 'json',
        beforeSend: function() {
          $('#button-login').button('loading');
    },
        complete: function() {
            $('#button-login').button('reset');
        },
        success: function(json) {
            $('.alert, .text-danger').remove();
            $('.form-group').removeClass('has-error');

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#collapse-checkout-option .panel-body').prepend('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');

        // Highlight any found errors
        $('input[name=\'email\']').parent().addClass('has-error');
        $('input[name=\'password\']').parent().addClass('has-error');
       }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Register
$(document).delegate('#button-register', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/register/save',
        type: 'post',
        data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address textarea, #collapse-payment-address select'),
        dataType: 'json',
        beforeSend: function() {
      $('#button-register').button('loading');
    },
        success: function(json) {
            $('.alert, .text-danger').remove();
            $('.form-group').removeClass('has-error');

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-register').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-payment-address .panel-body').prepend('<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> ' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

        for (i in json['error']) {
          var element = $('#input-payment-' + i.replace('_', '-'));

          if ($(element).parent().hasClass('input-group')) {
            $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
          } else {
            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
          }
        }

        // Highlight any found errors
        $('.text-danger').parent().addClass('has-error');
            } else {
                                var shipping_address = $('#payment-address input[name=\'shipping_address\']:checked').prop('value');

                if (shipping_address) {
                    $.ajax({
                        url: 'index.php?route=checkout/shipping_method',
                        dataType: 'html',
                        success: function(html) {
              // Add the shipping address
                            $.ajax({
                                url: 'index.php?route=checkout/shipping_address',
                                dataType: 'html',
                                success: function(html) {
                                    $('#collapse-shipping-address .panel-body').html(html);

                  $('#collapse-shipping-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 3 步： 配送地址 <i class="fa fa-caret-down"></i></a>');
                                },
                                error: function(xhr, ajaxOptions, thrownError) {
                                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                }
                            });

              $('#collapse-shipping-method .panel-body').html(html);

              $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-method" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 4 步： 配送方式 <i class="fa fa-caret-down"></i></a>');

                $('a[href=\'#collapse-shipping-method\']').trigger('click');

              $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('第 4 步： 配送方式');
              $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
              $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }
                    });
                } else {
                    $.ajax({
                        url: 'index.php?route=checkout/shipping_address',
                        dataType: 'html',
                        success: function(html) {
                            $('#collapse-shipping-address .panel-body').html(html);

              $('#collapse-shipping-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 3 步： 配送地址 <i class="fa fa-caret-down"></i></a>');

              $('a[href=\'#collapse-shipping-address\']').trigger('click');

              $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('第 4 步： 配送方式');
              $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
              $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }
                    });
                }
                
                $.ajax({
                    url: 'index.php?route=checkout/payment_address',
                    dataType: 'html',
                    complete: function() {
                        $('#button-register').button('reset');
                    },
                    success: function(html) {
                        $('#collapse-payment-address .panel-body').html(html);

            $('#collapse-payment-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 2 步： 賬單地址 <i class="fa fa-caret-down"></i></a>');
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Payment Address
$(document).delegate('#button-payment-address', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/payment_address/save',
        type: 'post',
        data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'password\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
        dataType: 'json',
        beforeSend: function() {
          $('#button-payment-address').button('loading');
    },
        complete: function() {
      $('#button-payment-address').button('reset');
        },
        success: function(json) {
            $('.alert, .text-danger').remove();

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                if (json['error']['warning']) {
                    $('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

        for (i in json['error']) {
          var element = $('#input-payment-' + i.replace('_', '-'));

          if ($(element).parent().hasClass('input-group')) {
            $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
          } else {
            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
          }
        }

        // Highlight any found errors
        $('.text-danger').parent().parent().addClass('has-error');
            } else {
                                $.ajax({
                    url: 'index.php?route=checkout/shipping_address',
                    dataType: 'html',
                    success: function(html) {
                        $('#collapse-shipping-address .panel-body').html(html);

            $('#collapse-shipping-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 3 步： 配送地址 <i class="fa fa-caret-down"></i></a>');

            $('a[href=\'#collapse-shipping-address\']').trigger('click');

            $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('第 4 步： 配送方式');
            $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
            $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
                
                $.ajax({
                    url: 'index.php?route=checkout/payment_address',
                    dataType: 'html',
                    success: function(html) {
                        $('#collapse-payment-address .panel-body').html(html);
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Shipping Address
$(document).delegate('#button-shipping-address', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/shipping_address/save',
        type: 'post',
        data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
        dataType: 'json',
        beforeSend: function() {
      $('#button-shipping-address').button('loading');
      },
        success: function(json) {
            $('.alert, .text-danger').remove();

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-shipping-address').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-warning">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

        for (i in json['error']) {
          var element = $('#input-shipping-' + i.replace('_', '-'));

          if ($(element).parent().hasClass('input-group')) {
            $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
          } else {
            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
          }
        }

        // Highlight any found errors
        $('.text-danger').parent().parent().addClass('has-error');
            } else {
                $.ajax({
                    url: 'index.php?route=checkout/shipping_method',
                    dataType: 'html',
                    complete: function() {
                        $('#button-shipping-address').button('reset');
                    },
                    success: function(html) {
                        $('#collapse-shipping-method .panel-body').html(html);

            $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-method" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 4 步： 配送方式 <i class="fa fa-caret-down"></i></a>');

            $('a[href=\'#collapse-shipping-method\']').trigger('click');

            $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
            $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');

                        $.ajax({
                            url: 'index.php?route=checkout/shipping_address',
                            dataType: 'html',
                            success: function(html) {
                                $('#collapse-shipping-address .panel-body').html(html);
                            },
                            error: function(xhr, ajaxOptions, thrownError) {
                                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                            }
                        });
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });

                $.ajax({
                    url: 'index.php?route=checkout/payment_address',
                    dataType: 'html',
                    success: function(html) {
                        $('#collapse-payment-address .panel-body').html(html);
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Guest
$(document).delegate('#button-guest', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/guest/save',
        type: 'post',
        data: $('#collapse-payment-address input[type=\'text\'], #collapse-payment-address input[type=\'date\'], #collapse-payment-address input[type=\'datetime-local\'], #collapse-payment-address input[type=\'time\'], #collapse-payment-address input[type=\'checkbox\']:checked, #collapse-payment-address input[type=\'radio\']:checked, #collapse-payment-address input[type=\'hidden\'], #collapse-payment-address textarea, #collapse-payment-address select'),
        dataType: 'json',
        beforeSend: function() {
          $('#button-guest').button('loading');
      },
        success: function(json) {
            $('.alert, .text-danger').remove();

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-guest').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-payment-address .panel-body').prepend('<div class="alert alert-warning">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

        for (i in json['error']) {
          var element = $('#input-payment-' + i.replace('_', '-'));

          if ($(element).parent().hasClass('input-group')) {
            $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
          } else {
            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
          }
        }

        // Highlight any found errors
        $('.text-danger').parent().addClass('has-error');
            } else {
                                var shipping_address = $('#collapse-payment-address input[name=\'shipping_address\']:checked').prop('value');

                if (shipping_address) {
                    $.ajax({
                        url: 'index.php?route=checkout/shipping_method',
                        dataType: 'html',
                        complete: function() {
                            $('#button-guest').button('reset');
                        },
                        success: function(html) {
              // Add the shipping address
                            $.ajax({
                                url: 'index.php?route=checkout/guest_shipping',
                                dataType: 'html',
                                success: function(html) {
                                    $('#collapse-shipping-address .panel-body').html(html);

                  $('#collapse-shipping-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 3 步： 配送地址 <i class="fa fa-caret-down"></i></a>');
                                },
                                error: function(xhr, ajaxOptions, thrownError) {
                                    alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                                }
                            });

                $('#collapse-shipping-method .panel-body').html(html);

              $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-method" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 4 步： 配送方式 <i class="fa fa-caret-down"></i></a>');

              $('a[href=\'#collapse-shipping-method\']').trigger('click');

              $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
              $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }
                    });
                } else {
                    $.ajax({
                        url: 'index.php?route=checkout/guest_shipping',
                        dataType: 'html',
                        complete: function() {
                            $('#button-guest').button('reset');
                        },
                        success: function(html) {
                            $('#collapse-shipping-address .panel-body').html(html);

              $('#collapse-shipping-address').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-address" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 3 步： 配送地址 <i class="fa fa-caret-down"></i></a>');

              $('a[href=\'#collapse-shipping-address\']').trigger('click');

              $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('第 4 步： 配送方式');
              $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
              $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                        },
                        error: function(xhr, ajaxOptions, thrownError) {
                            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                        }
                    });
                }
                            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

// Guest Shipping
$(document).delegate('#button-guest-shipping', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/guest_shipping/save',
        type: 'post',
        data: $('#collapse-shipping-address input[type=\'text\'], #collapse-shipping-address input[type=\'date\'], #collapse-shipping-address input[type=\'datetime-local\'], #collapse-shipping-address input[type=\'time\'], #collapse-shipping-address input[type=\'password\'], #collapse-shipping-address input[type=\'checkbox\']:checked, #collapse-shipping-address input[type=\'radio\']:checked, #collapse-shipping-address textarea, #collapse-shipping-address select'),
        dataType: 'json',
        beforeSend: function() {
          $('#button-guest-shipping').button('loading');
    },
        success: function(json) {
            $('.alert, .text-danger').remove();

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-guest-shipping').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-shipping-address .panel-body').prepend('<div class="alert alert-danger">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }

        for (i in json['error']) {
          var element = $('#input-shipping-' + i.replace('_', '-'));

          if ($(element).parent().hasClass('input-group')) {
            $(element).parent().after('<div class="text-danger">' + json['error'][i] + '</div>');
          } else {
            $(element).after('<div class="text-danger">' + json['error'][i] + '</div>');
          }
        }

        // Highlight any found errors
        $('.text-danger').parent().addClass('has-error');
            } else {
                $.ajax({
                    url: 'index.php?route=checkout/shipping_method',
                    dataType: 'html',
                    complete: function() {
                        $('#button-guest-shipping').button('reset');
                    },
                    success: function(html) {
                        $('#collapse-shipping-method .panel-body').html(html);

            $('#collapse-shipping-method').parent().find('.panel-heading .panel-title').html('<a href="#collapse-shipping-method" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 4 步： 配送方式 <i class="fa fa-caret-down"></i>');

            $('a[href=\'#collapse-shipping-method\']').trigger('click');

            $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('第 5 步： 支付方式');
            $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$(document).delegate('#button-shipping-method', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/shipping_method/save',
        type: 'post',
        data: $('#collapse-shipping-method input[type=\'radio\']:checked, #collapse-shipping-method textarea'),
        dataType: 'json',
        beforeSend: function() {
          $('#button-shipping-method').button('loading');
    },
        success: function(json) {
            $('.alert, .text-danger').remove();

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-shipping-method').button('reset');

                if (json['error']['warning']) {
                    $('#collapse-shipping-method .panel-body').prepend('<div class="alert alert-danger">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }
            } else {
                $.ajax({
                    url: 'index.php?route=checkout/payment_method',
                    dataType: 'html',
                    complete: function() {
                        $('#button-shipping-method').button('reset');
                    },
                    success: function(html) {
                        $('#collapse-payment-method .panel-body').html(html);

            $('#collapse-payment-method').parent().find('.panel-heading .panel-title').html('<a href="#collapse-payment-method" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 5 步： 支付方式 <i class="fa fa-caret-down"></i></a>');

            $('a[href=\'#collapse-payment-method\']').trigger('click');

            $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('第 6 步： 確認訂單');
                    },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});

$(document).delegate('#button-payment-method', 'click', function() {
    $.ajax({
        url: 'index.php?route=checkout/payment_method/save',
        type: 'post',
        data: $('#collapse-payment-method input[type=\'radio\']:checked, #collapse-payment-method input[type=\'checkbox\']:checked, #collapse-payment-method textarea'),
        dataType: 'json',
        beforeSend: function() {
          $('#button-payment-method').button('loading');
    },
        success: function(json) {
            $('.alert, .text-danger').remove();

            if (json['redirect']) {
                location = json['redirect'];
            } else if (json['error']) {
                $('#button-payment-method').button('reset');
                
                if (json['error']['warning']) {
                    $('#collapse-payment-method .panel-body').prepend('<div class="alert alert-danger">' + json['error']['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
                }
            } else {
                $.ajax({
                    url: 'index.php?route=checkout/confirm',
                    dataType: 'html',
                    complete: function() {
                        $('#button-payment-method').button('reset');
                    },
                    success: function(html) {
                        $('#collapse-checkout-confirm .panel-body').html(html);

            $('#collapse-checkout-confirm').parent().find('.panel-heading .panel-title').html('<a href="#collapse-checkout-confirm" data-toggle="collapse" data-parent="#accordion" class="accordion-toggle">第 6 步： 確認訂單 <i class="fa fa-caret-down"></i></a>');

            $('a[href=\'#collapse-checkout-confirm\']').trigger('click');
          },
                    error: function(xhr, ajaxOptions, thrownError) {
                        alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                    }
                });
            }
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
        }
    });
});
//--></script>